﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TTData;

namespace TTMain.Data
{
    public class MapObjects
    {
        private ArrayList _houseList,  _cityCardList,  _displayCardList, _discardedCardList;
        private List<BonusCard> _bonusCardList;
        private List<CarrigeCard> _carriegeList;

        private int[,] _matrix = { { 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, 
                                 { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
                                 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, 
                                 { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, 
                                 { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 }, 
                                 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0 }, 
                                 { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, 
                                 { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1 }, 
                                 { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0 }, 
                                 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, 
                                 { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, 
                                 { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0 }, 
                                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0 }, 
                                 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, 
                                 { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, 
                                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, 
                                 { 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, 
                                 { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1 }, 
                                 { 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0 }, 
                                 { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 }, 
                                 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, 
                                 { 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 } };

        public MapObjects(Int32 numberOfPlayers)
        {
            // Generate all the carrige cards in the game

            _carriegeList = new List<CarrigeCard>();

            CarrigeCard c = null;

            for (int i = 3; i < 8; i++)
            {
                for (int j = 0; j < numberOfPlayers; j++)
                {
                    switch (i)
                    {
                        case 3:
                            c = new CarrigeCard(i, 2);
                            _carriegeList.Add(c);
                            break;
                        case 4:
                            c = new CarrigeCard(i, 3);
                            _carriegeList.Add(c);
                            break;
                        case 6:
                            c = new CarrigeCard(i, 7);
                            _carriegeList.Add(c);
                            break;
                        case 7:
                            c = new CarrigeCard(i, 10);
                            _carriegeList.Add(c);
                            break;
                        default:
                            c = new CarrigeCard(i, i);
                            _carriegeList.Add(c);
                            break;
                    }
                }
            }

            // Generate all the houses in the game
            _houseList = new ArrayList();

            House h = null;
            for (int i = 0; i < numberOfPlayers; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    switch (i)
                    {
                        case 0:
                            h = new House(House.COLOR_RED);
                            break;
                        case 1:
                            h = new House(House.COLOR_BLUE);
                            break;
                        case 2:
                            h = new House(House.COLOR_GREEN);
                            break;
                        case 3:
                            h = new House(House.COLOR_YELLOW);
                            break;
                        default:
                            break;
                    }
                }
            }

            // Generate all the city cards in the game
            _cityCardList = new ArrayList();

            CityCard cc = null;
            String[] cities = { City.AUGSBURG, City.BASEL, City.BUDWEIS, City.CARLSRUHE, City.FREIBURG, City.INGOLSTADT, 
                                  City.INNSBRUCK, City.KEMPTEN, City.LINZ, City.LODZ, City.MANNHEIM, City.MUNCHEN, City.NURENBERG, 
                                  City.PASSAU, City.PILSEN, City.REGENSBURG, City.SALZBURG, City.SIGMARINGEN, City.STUTTGART, 
                                  City.ULM, City.WURZBURG, City.ZURICH };

            for (int i = 0; i < cities.Length; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    cc = new CityCard(cities[i]);
                    _cityCardList.Add(cc);
                }
            }

            // Generate all the bonuses in the game
            _bonusCardList = new List<BonusCard>();

            BonusCard b = null;
            for (int i = 0; i < 4; i++)
            {
                b = new BonusCard(BonusCard.BONUS_ROAD_LENGTH_7, BonusCard.MAX_ROAD_LENGTH_7 - i);
                _bonusCardList.Add(b);
                b = new BonusCard(BonusCard.BONUS_REGION_ALL, BonusCard.MAX_REGION_ALL - i);
                _bonusCardList.Add(b);
                b = new BonusCard(BonusCard.BONUS_REGION_BAIERN, BonusCard.MAX_REGION_BAIERN - i);
                _bonusCardList.Add(b);
            }
            for (int i = 0; i < 3; i++)
            {
                b = new BonusCard(BonusCard.BONUS_ROAD_LENGTH_6, BonusCard.MAX_ROAD_LENGTH_6 - i);
                _bonusCardList.Add(b);
                b = new BonusCard(BonusCard.BONUS_REGION_BADEN, BonusCard.MAX_REGION_BADEN - i);
                _bonusCardList.Add(b);
                b = new BonusCard(BonusCard.BONUS_REGION_WURTT_HOHENZ, BonusCard.MAX_REGION_WURTT_HOHENZ - i);
                _bonusCardList.Add(b);
                b = new BonusCard(BonusCard.BONUS_REGION_SCHWEIZ_TYROL, BonusCard.MAX_REGION_SCHWEIZ_TYROL - i);
                _bonusCardList.Add(b);
                b = new BonusCard(BonusCard.BONUS_REGION_BOHEM_SALZB, BonusCard.MAX_REGION_BOHEM_SALZB - i);
                _bonusCardList.Add(b);
            }
            for (int i = 0; i < 2; i++)
            {
                b = new BonusCard(BonusCard.BONUS_ROAD_LENGTH_5, BonusCard.MAX_ROAD_LENGTH_5 - i);
                _bonusCardList.Add(b);
            }
            b = new BonusCard(BonusCard.BONUS_FINISH_GAME, BonusCard.MAX_FINISH_GAME);
            _bonusCardList.Add(b);

            _displayCardList = new ArrayList();
            _discardedCardList = new ArrayList();
        }

        /// <summary>
        /// Sets or gets the house list for the game
        /// </summary>
        public ArrayList HouseList
        {

            get
            {
                return _houseList;
            }
            set
            {
                _houseList = value;
            }
        }

        /// <summary>
        /// Sets or gets the carrige cards for the game
        /// </summary>
        public List<CarrigeCard> CarriegeList
        {

            get
            {
                return _carriegeList;
            }
            set
            {
                _carriegeList = value;
            }
        }

        /// <summary>
        /// Sets or gets the city cards for the game
        /// </summary>
        public ArrayList CityCardList
        {

            get
            {
                return _cityCardList;
            }
            set
            {
                _cityCardList = value;
            }
        }

        /// <summary>
        /// Sets or gets the bonus cards for the game
        /// </summary>
        public List<BonusCard> BonusCardList
        {

            get
            {
                return _bonusCardList;
            }
            set
            {
                _bonusCardList = value;
            }
        }

        /// <summary>
        /// Sets or gets the display card list for the game
        /// </summary>
        public ArrayList DisplayCardList
        {

            get
            {
                return _displayCardList;
            }
            set
            {
                _displayCardList = value;
            }
        }

        /// <summary>
        /// Sets or gets the display card list for the game
        /// </summary>
        public ArrayList DiscardedCardList
        {

            get
            {
                return _discardedCardList;
            }
            set
            {
                _discardedCardList = value;
            }
        }

        /// <summary>
        /// Sets or gets the display card list for the game
        /// </summary>
        public int[,] Matrix
        {

            get
            {
                return _matrix;
            }
            set
            {
                _matrix = value;
            }
        }

        public BonusCard ExtractBonusCard(Int32 bonusType)
        {
            for (int i = 0; i < _bonusCardList.Count; i++)
            {
                if (_bonusCardList[i].Type == bonusType)
                {
                    BonusCard c = _bonusCardList[i];
                    _bonusCardList.RemoveAt(i);
                    return c;
                }
            }
            return null;
        }

        public BonusCard GetBonusCard(Int32 bonusType)
        {
            for (int i = 0; i < _bonusCardList.Count; i++)            
                if (_bonusCardList[i].Type == bonusType)                   
                    return _bonusCardList[i];;                            
            return null;
        }

        public CarrigeCard ExtractCarrigeCard(Int32 value)
        {
            for (int i = 0 ; i< _carriegeList.Count; i++)
                if (_carriegeList[i].Value == value)
                {
                    CarrigeCard c = _carriegeList[i];
                    _carriegeList.RemoveAt(i);
                    return c;
                }
            return null;
        }

        public CarrigeCard GetCarrigeCard(Int32 value)
        {
            for (int i = 0; i < _carriegeList.Count; i++)
                if (_carriegeList[i].Value == value)                
                    return _carriegeList[i];        
            return null;
        }
        
    }
}
